package com.example.activiti.service.impl;

import com.example.activiti.domain.ActWorkflowFormData;
import com.example.activiti.domain.dto.HistoryDataDTO;
import com.example.activiti.domain.dto.HistoryFormDataDTO;
import com.example.activiti.service.IActWorkflowFormDataService;
import com.example.activiti.service.IFormHistoryDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 汇讯数码科技(深圳)有限公司
 * 创建日期:2020/11/3-10:20
 * 版本   开发者     日期
 * 1.0    Danny    2020/11/3
 *
 * @author Administrator
 */
@Service
public class FormHistoryDataServiceImpl implements IFormHistoryDataService {
    @Resource
    private IActWorkflowFormDataService actWorkflowFormDataService;


    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<HistoryDataDTO> historyDataShow(String businessKey) {
        List<HistoryDataDTO> returnHistoryFromDataDTOS = new ArrayList<>();
        List<ActWorkflowFormData> actWorkflowFormData = actWorkflowFormDataService.selectActWorkflowFormDataByBusinessKey(businessKey);
        Map<String, List<ActWorkflowFormData>> collect = actWorkflowFormData.stream().collect(Collectors.groupingBy(ActWorkflowFormData::getTaskNodeName));
        collect.forEach((key, value) -> {
            HistoryDataDTO returnHistoryFromDataDTO = new HistoryDataDTO();
            returnHistoryFromDataDTO.setTaskNodeName(value.get(0).getTaskNodeName());
            returnHistoryFromDataDTO.setCreateName(value.get(0).getCreateName());
            returnHistoryFromDataDTO.setCreatedDate(sdf.format(value.get(0).getCreateTime()));
            returnHistoryFromDataDTO.setFormHistoryDataDTO(value.stream().map(awfd -> new HistoryFormDataDTO(awfd.getControlName(), awfd.getControlValue())).collect(Collectors.toList()));
            returnHistoryFromDataDTOS.add(returnHistoryFromDataDTO);
        });
        return returnHistoryFromDataDTOS.stream().sorted(Comparator.comparing(HistoryDataDTO::getCreatedDate)).collect(Collectors.toList());
    }


}
